package com.updateeproject.controller;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.updateeproject.dataobject.UserDO;
import com.updateeproject.response.CommonReturnType;
import com.updateeproject.service.UserService;
import com.updateeproject.utils.AliyunMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private HttpServletRequest httpServletRequest;

    @RequestMapping("/get01")
    @ResponseBody
    public UserDO getUser01(Integer id) {
        UserDO userDO = userService.getUserById(id);
        return userDO;
    }

    @RequestMapping("/get02")
    @ResponseBody
    public UserDO getUser02(@RequestParam("uid") Integer id) {
        UserDO userDO = userService.getUserById(id);
        return userDO;
    }

    @RequestMapping("/register")
    @ResponseBody
//    public UserDO register(String telphone,String otpCode, HttpSession session) {
    public UserDO register(String telphone,String otpCode) {
        HttpSession session = httpServletRequest.getSession();
        String otpCodeOfSession = (String) session.getAttribute(telphone);
        if (otpCodeOfSession.equals(otpCode)) {
            UserDO userDO = new UserDO();
            userDO.setId(653);
            userDO.setGender((byte) 1);
            userDO.setName("hh");
            userDO.setTelphone(telphone);
            userDO.setRegisterMode("telphone");
            userDO.setThirdPartyId("happy");
            return userDO;
        }
        return null;
    }


    //用户获取otp短信接口
    @RequestMapping(value = "/getOtp", method = RequestMethod.GET)
    @ResponseBody
//    public void getOtp(@RequestParam(name = "telphone") String telphone, HttpSession session) {
    public Map<String,String> getOtp(@RequestParam(name = "telphone") String telphone) throws ClientException {
        //需要按照一定的规则生成OTP验证码
        Random random = new Random();
        int randomInt = random.nextInt(99999);
        randomInt += 10000;
        String otpCode = String.valueOf(randomInt);
        HttpSession session = httpServletRequest.getSession();
        //将OTP验证码同对应的手机号关联,使用httpsession的方式绑定他的手机号与OTPCODE
        session.setAttribute(telphone, otpCode);
//        this.httpServletRequest.getSession().setAttribute(telphone, otpCode);


        //将OTP验证码通过短信通道发送给用户，省略
        // System.out.println("telphone = "+telphone+"&otpCode = "+otpCode+this.httpServletRequest.getSession().getAttribute(telphone));
//        SendSms.sendMessage(telphone, otpCode);
        AliyunMessageUtil.sendSms(telphone, otpCode);
        log.info(otpCode);
        Map<String,String> map = new HashMap<>();
        map.put("msg", "快看你手机！");
        return map;
    }



}
